﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;
using weikebuluo.Model;
using weikebuluo.DAL;

namespace weikeweixin.web.admin.wechat
{
    public partial class keyword : weikeweixin.web.admin.cs.adminbase
    {
        w_keyword_dal keyworddal = new w_keyword_dal();
        protected w_keyword_model keywordmodel = new w_keyword_model();

        w_article_dal articledal = new w_article_dal();
        protected DataTable dtarticle = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            dtarticle = articledal.GetList("useer_id=" + user_id + " and article_layid=0").Tables[0];
            

            if (!string.IsNullOrEmpty(Request.QueryString["id"])) {
                keywordmodel = keyworddal.GetModel(int.Parse(Request.QueryString["id"]));
            }
            switch (Request.Form["action"]) { 
                case"keyword":
                    keyadd();
                    break;
            }
        }
        //
        private void keyadd() {
            if (!string.IsNullOrEmpty(Request.Form["id"])) {
                keywordmodel = keyworddal.GetModel(int.Parse(Request.Form["id"]));
            }
            keywordmodel.keyword_name = Request.Form["keyword_name"].Trim().ToString();
            keywordmodel.keyword_type = Request.Form["keyword_type"].Trim().ToString();
            

            switch (Request.Form["keyword_type"])
            {
                case "text":
                    keywordmodel.keyword_text = Request.Form["keyword_text"].Trim().ToString();
                    break;
                case "news":
                    keywordmodel.article_id = int.Parse(Request.Form["article_id"].Trim().ToString());
                    break;
            }
            keywordmodel.wechat_id = int.Parse(wechat_id);
            if (!string.IsNullOrEmpty(Request.Form["id"]))
            {
                bool re = keyworddal.Update(keywordmodel);
                if (re)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"keywordlist.aspx\",\"error\":\"\"}");
                    Response.End();
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"keyword.aspx\",\"error\":\"\"}");
                    Response.End();
                }
            }
            else {
                keywordmodel.keyword_addtime = DateTime.Now;
                int re = keyworddal.Add(keywordmodel);
                if (re > 0)
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存成功！\",\"url\":\"keywordlist.aspx\",\"error\":\"\"}");
                    Response.End();
                }
                else
                {
                    Response.Write("{\"errno\":\"0\",\"tip\":\"保存失败！\",\"url\":\"keyword.aspx\",\"error\":\"\"}");
                    Response.End();
                }
            }
            
        }
    }
}